package com.winai.launcher.manager;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.media.AudioManager;

import com.ks.projectbasictools.ActivityCollector;
import com.ks.projectbasictools.AppManager;
import com.ks.projectbasictools.base.SimpleView;
import com.winai.launcher.R;
import com.winai.launcher.override.VolumeWindowCountTimer;
import com.winai.launcher.override.VolumnView;
import com.winai.launcher.view.set.SetVoiceActivity;
import com.winai.launcher.view.timeManager.AlarmAlertActivity;
import com.yhao.floatwindow.FloatActivity;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.Screen;

public class VolumeWindow {
    private static VolumeWindow mInstance;
    private Context mContext;
    /**
     * 音量延时消失时间
     */
    private static final int DELAY_HIDE_PERIOD = 4000;

    private VolumnView mVolumnView;
    /**
     * 最大音量值
     */
    private int mMaxVolume;

    public static VolumeWindow getInstance(Context context) {
        if (mInstance == null) {
            synchronized (VolumeWindow.class) {
                if (mInstance == null) {
                    mInstance = new VolumeWindow(context);
                }
            }
        }
        return mInstance;
    }

    public VolumeWindow() {
        try {
            throw new Exception("VolumeWindow 不允许实例化");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private VolumeWindow(Context context) {
        init(context);
    }

    private void init(Context context) {
        mContext = context.getApplicationContext();

        SimpleView simpleView = new SimpleView(mContext, null, R.style.volume_adjust_dialog_anim, R.layout.volume_layout);
        AudioManager audioManager = (AudioManager) mContext.getSystemService(Service.AUDIO_SERVICE);
        mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        mVolumnView = simpleView.findViewById(R.id.volumn);

        FloatWindow
                .with(mContext)
                .setView(simpleView)
//                .setWidth(Screen.width, 0.2f) //设置悬浮控件宽高
//                .setHeight(Screen.width, 0.2f)
                .setX(Screen.width, 0.435f)
                .setY(Screen.height, 0.35f)
                .setMoveType(MoveType.inactive, 0, 0)
                .setFilter(true, FloatActivity.class)/*这行代码必须要有，类名可以是任意类*/
//                .setMoveStyle(500, new BounceInterpolator())
//                .setViewStateListener(mViewStateListener)
//                .setPermissionListener(mPermissionListener)
//                .setDesktopShow(false)
                .build();
    }

    public void show(int progress) {
        //声音设置界面不弹出悬浮窗
        if (needShow()) {
            mVolumnView.setProgress(progress);
            /* mProgressBar.post(() -> mProgressBar.setProgress(progress));*/
            if (FloatWindow.get() != null && !FloatWindow.get().isShowing()) {
                FloatWindow.get().show();
            }
            //倒计时重置
            VolumeWindowCountTimer.getInstance(mContext, DELAY_HIDE_PERIOD).restart();
        }
    }

    private boolean needShow() {
        try {
            Activity activity = AppManager.getAppManager().currentActivity();
            if (activity == ActivityCollector.getActivity(SetVoiceActivity.class)
                    || activity == ActivityCollector.getActivity(AlarmAlertActivity.class)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

//    public void show(boolean isMute, int progress) {
//        //声音设置界面不弹出悬浮窗
//        Activity activity = AppManager.getAppManager().currentActivity();
//        if (activity != ActivityCollector.getActivity(SetVoiceActivity.class)) {
//            mVolumnView.setProgress(isMute ? 0 : progress);
//            /* mProgressBar.post(() -> mProgressBar.setProgress(progress));*/
//            if (FloatWindow.get() != null && !FloatWindow.get().isShowing()) {
//                FloatWindow.get().show();
//            }
//            //倒计时重置
//            VolumeWindowCountTimer.getInstance(mContext, DELAY_HIDE_PERIOD).restart();
//        }
//    }

    public void destroy() {
        if (FloatWindow.get() != null && FloatWindow.get().isShowing()) {
            FloatWindow.get().hide();
        }
        VolumeWindowCountTimer.getInstance(mContext, DELAY_HIDE_PERIOD).release();
    }
}
